package top.huntwolf.admin.utils;

import java.io.*;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.Base64;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class QiniuCloudUtil {

    // 设置需要操作的账号的AK和SK
    private static final String ACCESS_KEY = "";
    private static final String SECRET_KEY = "-";

    // 要上传的空间
    private static final String bucketname = "";

    // 密钥
    private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

    //默认不指定key的情况下，以文件内容的hash值作为文件名
    private static final String key = null;

    //新建空间时，七牛云分配出的域名
    private static final String DOMAIN = "http://ra0faxbpa.hn-bkt.clouddn.com/";

    public static String getUpToken() {
        return auth.uploadToken(bucketname, key, 3600, new StringMap().put("insertOnly", 1));
    }

    // 普通上传
    public static String uploadCommon(String filePath, String fileName) throws IOException {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        // 创建上传对象
        UploadManager uploadManager = new UploadManager(cfg);
        String upToken = getUpToken();
        try {
            Response response = uploadManager.put(filePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//            System.out.println(putRet.key);
//            System.out.println(putRet.hash);
//            System.out.println(DOMAIN + putRet.hash);
            return DOMAIN + putRet.key;

        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }

        return null;
    }


    //base64方式上传
    public static String put64image(byte[] base64, String key) throws Exception {
        String file64 = Base64.encodeToString(base64, 0);
        Integer len = base64.length;

        //华南空间使用 upload-z2.qiniu.com
        String url = "http://upload-z2.qiniu.com/putb64/" + len + "/key/" + UrlSafeBase64.encodeToString(key);

        RequestBody rb = RequestBody.create(null, file64);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "application/octet-stream")
                .addHeader("Authorization", "UpToken " + getUpToken())
                .post(rb).build();
        OkHttpClient client = new OkHttpClient();

        okhttp3.Response response = client.newCall(request).execute();
//        System.out.println(response);
        //用此地址可在网页中访问到上传的图片
        return DOMAIN + key;
    }
}